﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Ithalat
{
    public partial class MasrafByFaturaId : XForm
    {
        public MasrafByFaturaId()
        {
            InitializeComponent();
        }

        string REF
        {
            get
            {
                return "KRB.T6";    // +(((DateTime)beiAy1.EditValue).Year % 10).ToStr();
            }
        }

        private void MasrafByFaturaId_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetImportList", REF);
            gridView1.MakeReadOnly();
            gridView1.FormatAsPercentage("Oran");
            gridView1.FormatAsNumber(1, "ToplamMasraf", "LineTotals", "FaturaToplami");
            gridView1.AddSummary("ToplamMasraf", "LineTotals", "FaturaToplami");

            foreach (DataRow r in AppSession.Lookups.HesapPlani.Rows)
            {
                string hp = r["HesapPlaniId"].ToStr();
                if (gridView1.Columns.Contains(gridView1.Columns[hp]))
                {
                    gridView1.FormatAsNumber(1, hp);
                    gridView1.AddSummary(hp);
                    //gridView1.Columns[hp].Width = 70;
                    gridView1.SetCaptions(hp + ":" + r["Abbr"].ToStr());
                    gridView1.Columns[hp].ToolTip = r["HesapKodu"].ToStr() + ". " + r["Name"].ToStr();
                }
            }
            gridView1.BestFitColumns();

            //gridView1.CustomDrawColumnHeader += gridView1_CustomDrawColumnHeader;
            //SetBestHeaderHeight(gridView1);
        }


        private void gridView1_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && !e.Column.Visible)
            {
                return;
            }
            DrawVertical(e);
        }

        private static void DrawVertical(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            Rectangle r;
            r = e.Info.CaptionRect;
            e.Info.Caption = string.Empty;
            e.Painter.DrawObject(e.Info);
            if (e.Column != null)
            {
                var state = e.Graphics.Save();
                var sf = new StringFormat();
                sf.Trimming = StringTrimming.EllipsisCharacter;
                sf.FormatFlags |= StringFormatFlags.NoWrap;
                sf.FormatFlags |= StringFormatFlags.DirectionVertical;
                e.Graphics.DrawString(e.Column.GetTextCaption(), e.Appearance.Font, e.Appearance.GetForeBrush(e.Cache), r, sf);
                e.Graphics.Restore(state);
            }
            e.Handled = true;
        }

        public static void SetBestHeaderHeight(GridView view)
        {
            view.BeginUpdate();
            foreach (GridColumn col in view.VisibleColumns)
            {
                int height = GetVerticalTextHeight(view, col.Caption) + 8;
                view.ColumnPanelRowHeight = Math.Max(height, view.ColumnPanelRowHeight);
            }
            view.EndUpdate();
        }

        private static int GetVerticalTextHeight(GridView view, string text)
        {
            DevExpress.Utils.AppearanceObject app = view.PaintAppearance.HeaderPanel;
            StringFormat sf = new StringFormat();
            sf.FormatFlags = StringFormatFlags.DirectionVertical;
            SizeF size = view.GridControl.CreateGraphics().MeasureString(text, app.GetFont(), 1000, sf);
            return size.ToSize().Height;
        }

    }
}
